package com.hqs.drawingboard;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hqs.common.utils.ScreenUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * Created by super on 2017/7/4.
 */

public class ColorPickerView extends RelativeLayout implements View.OnClickListener {


    private LinearLayout contentView;
    private OnPickColorClickListener onPickColorClickListener;
    private int[] colors;
    private OnClickListener onClickListener;

    public ColorPickerView(Context context) {
        super(context);
        initView();
    }

    public ColorPickerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        if (contentView == null) {
            contentView = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.pick_color_view, null);
            LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.addRule(ALIGN_PARENT_BOTTOM, TRUE);
            layoutParams.bottomMargin = (int) (4 * ScreenUtils.density(getContext()));
            contentView.setLayoutParams(layoutParams);
            contentView.setBackgroundColor(Color.WHITE);
            this.addView(contentView);

            colors = getContext().getResources().getIntArray(R.array.colors);

            int count = contentView.getChildCount();
            for (int i = 0; i < count; i++) {
                LinearLayout linearLayout = (LinearLayout) contentView.getChildAt(i);
                int c = linearLayout.getChildCount();
                for (int j = 0; j < c; j++) {
                    TextView textView = (TextView) linearLayout.getChildAt(j);
                    textView.setBackgroundColor(colors[i * 4 + j]);
                    textView.setTag(i * 4 + j);
                    textView.setOnClickListener(this);
                }
            }
        }
    }

    @Override
    public void setOnClickListener(OnClickListener onClickListener) {
        super.setOnClickListener(onClickListener);
        this.onClickListener = onClickListener;
    }

    public void setOnPickColorClickListener(OnPickColorClickListener onPickColorClickListener) {
        this.onPickColorClickListener = onPickColorClickListener;
    }

    @Override
    public void onClick(View view) {
        int pos = (Integer)view.getTag();
        this.onPickColorClickListener.onPickColor(colors[pos]);
        this.onClickListener.onClick(view);
    }

    public interface OnPickColorClickListener{
        void onPickColor(int color);
    }
}
